package parsing.yago;

import core.objects.Category;
import core.objects.PublisherCategory;

public class TaxonomyParser extends YagoFileParser {

	private static final String TAXONOMY = "yagoTaxonomy.tsv";
	private static final int ESTIMATED_LINES_NUM = 451709;

	private static final String YAGO_RDF_SUBCLASSOF = "rdfs:subClassOf";
	private static final String WORDNET_BOOK = "wordnet book 106410904";
	private static final String WORDNET_NOVEL = "wordnet novel 106367879";
	private static final String WORDNET_PUBLISHER = "wordnet publisher 108062623";
	
	protected TaxonomyParser(String directory) {
		super(directory);
	}

	@Override
	protected String getFileName() {
		return TAXONOMY;
	}
	
	@Override
	protected int getEstimatedFileLinesNumber() {
		return ESTIMATED_LINES_NUM;
	}

	@Override
	protected void handleLineParsing(String[] line) {
		
		if (!isLineFromFileOkay(line)) {
			/* these are lines in the form of
			 * <wordnet_...> rdfs:subClassOf <wordnet_..>
			 * which are of no interest to us (they are marked
			 * as books have more specific wikicategory categories */
			return;
		}
		
		/* line is "<id> <something> rdfs:subClassOf <what_that_something_is>"*/
				
		if (line[3].equals(WORDNET_BOOK) || line[3].equals(WORDNET_NOVEL)) {
		
			ParsingEnvironment.getCategories().put(removeWikicategory(line[1]),
					new Category(0, removeWikicategory(line[1])));
			
		} else if (line[3].equals(WORDNET_PUBLISHER)) {

			String publisher = removeWikicategory(line[1]);
			ParsingEnvironment.getPublisherCategories().put(publisher,
					new PublisherCategory(0, publisher));
			
		}
	}

	@Override
	protected boolean isLineFromFileOkay(String[] line) {
		/* line should be "<..> <..> rdfs:subClassOf <..>" */
		return line.length == 4 && line[2].equals(YAGO_RDF_SUBCLASSOF);
	}

}
